package com.zuser.project.service.impl;


import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import com.amihaiemil.eoyaml.YamlMappingBuilder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuser.project.dao.CancelRuleDao;
import com.zuser.project.entity.CancelRuleEntity;
import com.zuser.project.service.CancelRuleService;
import com.zuser.project.util.CommonServiceUtil;
import com.zuser.project.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service("cancelRuleService")
public class CancelRuleServiceImpl extends ServiceImpl<CancelRuleDao, CancelRuleEntity> implements CancelRuleService {

    @Override
    public R queryPage(Map<String, Object> params) {
        CommonServiceUtil<CancelRuleEntity, CancelRuleServiceImpl> util = new CommonServiceUtil<>(this);
        return util.queryPage(params);
    }


    @Autowired
    private NacosConfigManager nacosConfigManager;
    @Value("${spring.profiles.active}")
    private String profile;
    private final String group = "DEFAULT_GROUP";

    /**
     * 更新Nacos配置
     *
     * @param newAccessToken
     * @return
     */
    public Boolean updateConfig(String newAccessToken) {
        try {
            // 构造 Nacos 配置的 dataId
            String dataId = String.format("test-%s.yml", profile);

            // 获取 Nacos 配置服务
            ConfigService configService = nacosConfigManager.getConfigService();

            // 读取当前配置内容
            String content = configService.getConfig(dataId, group, 5000);

            // 解析 YAML 配置
            YamlMapping mapping = Yaml.createYamlInput(content).readYamlMapping();
            YamlMapping express = mapping.yamlMapping("express");
            String accessToken = express.string("accessToken");

            // 如果两个 token 一样，则忽略修改
            if (newAccessToken.equals(accessToken)) {
                System.out.println("Token 未变化，忽略");
                return true;
            }

            // 手动构造新的 YAML 内容
            YamlMappingBuilder newExpressBuilder = Yaml.createYamlMappingBuilder()
                    .add("accessToken", newAccessToken);

            YamlMappingBuilder newMappingBuilder = Yaml.createYamlMappingBuilder()
                    .add("express", newExpressBuilder.build());

            // 转换为字符串
            String newYamlContent = newMappingBuilder.build().toString();

            // 更新 Nacos 配置
            boolean success = configService.publishConfig(dataId, group, newYamlContent);
            if (success) {
                System.out.println("Nacos 配置更新成功");
            } else {
                System.out.println("Nacos 配置更新失败");
            }
            return success;
        } catch (NacosException | IOException e) {
            e.printStackTrace();
        }
        return false;
    }



}